package cn.mrlong.contactutils;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ContactsHolder> {

    private Context context;
    private List<ContactInfo> datas;
    private RecyclerViewItemOnClick itemOnClick;

    public ContactsAdapter(Context context, List<ContactInfo> datas, RecyclerViewItemOnClick itemOnClick) {
        this.context = context;
        this.datas = datas;
        this.itemOnClick = itemOnClick;
    }

    @Override
    public ContactsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_contacts, parent, false);
        ContactsHolder holder = new ContactsHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ContactsHolder holder, int position) {
        holder.setViewData(position);

    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void addDatas(List<ContactInfo> contactInfoList) {
        this.datas.clear();
        this.datas.addAll(contactInfoList);
        notifyDataSetChanged();
    }

    class ContactsHolder extends RecyclerView.ViewHolder {

        private TextView name;
        private TextView phone;

        public ContactsHolder(View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(View itemView) {
            name = itemView.findViewById(R.id.name);
            phone = itemView.findViewById(R.id.phone);
        }

        public void setViewData(int position) {
            ContactInfo contactInfo = datas.get(position);
            name.setText(contactInfo.getName());
            phone.setText(contactInfo.getPhone());
        }
    }


}
